package gzh.repository;

import gzh.vo.ForbiddenWord;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Repository
public class ForbiddenWordRepository {

    private final List<ForbiddenWord> forbiddenWords = new CopyOnWriteArrayList<>();
    private final AtomicLong idGenerator = new AtomicLong(0); // 用于生成唯一ID

    public List<ForbiddenWord> findAll() {
        return forbiddenWords;
    }

    public void save(ForbiddenWord word) {
        if (word.getId() == null) { // 如果是新添加的词，赋予其一个新的ID
            word.setId(idGenerator.incrementAndGet());
        }
        forbiddenWords.add(word);
    }

    public void deleteById(Long id) {
        forbiddenWords.removeIf(w -> w.getId().equals(id));
    }

    public ForbiddenWord findById(Long id) {
        return forbiddenWords.stream()
                .filter(w -> w.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    // 新增：获取所有敏感词
    public List<String> getForbiddenWords() {
        return forbiddenWords.stream()
                .map(ForbiddenWord::getWord)
                .collect(Collectors.toList());
    }
}